package com.lianniu.web.buyerOrder.controller;

import com.lianniu.web.operationMessage.service.OperMessageService;
import com.lianniu.web.operationMessage.vo.OperMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianniu.web.base.BaseResult;
import com.lianniu.web.baseService.user.service.ISupplyService;
import com.lianniu.web.buyerOrder.model.BuyerOrderConditionVo;
import com.lianniu.web.buyerOrder.model.CancelOrder;
import com.lianniu.web.buyerOrder.model.OrderCommentVo;
import com.lianniu.web.buyerOrder.model.OrderInfoVo;
import com.lianniu.web.buyerOrder.service.IBuyerOrderService;
import com.lianniu.web.buyerOrder.service.IOrderCommentService;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/rest/buyerOrder")
public class BuyerOrderRestController {

    @Autowired
    IBuyerOrderService buyerOrderService;
    @Autowired
    ISupplyService sellerService;
    @Autowired
    IOrderCommentService orderCommentService;

    @Autowired
    OperMessageService operMessageService;

    @RequestMapping("/getOrderInfo")
    public
    @ResponseBody
    BaseResult getOrderInfo(String orderid) {

        BaseResult result = buyerOrderService.getOrderInfo(orderid);
        return result;
    }

    @RequestMapping("/getOrders")
    public
    @ResponseBody
    BaseResult getOrders(BuyerOrderConditionVo condition) {
        BaseResult result = buyerOrderService.getOrders(condition);

        return result;
    }

    @RequestMapping("/cancelOrder")
    public
    @ResponseBody
    BaseResult cancelOrder(CancelOrder cancel) {
        BaseResult result = buyerOrderService.cancelOrder(cancel);
        return result;
    }

    @RequestMapping("/payMoney")
    public
    @ResponseBody
    BaseResult payMoney(CancelOrder pay) {

        return buyerOrderService.payMoney(pay);
    }

    @RequestMapping("/confirmOrder")
    public
    @ResponseBody
    BaseResult confirmOrder(CancelOrder confirm) {
        buyerOrderService.confirmOrder(confirm);
        return BaseResult.success();
    }

    /**
     * @param sure
     * @param comment
     * @param comResult
     * @return
     * @Title: sureOrder
     * @Description: 评论
     * @date: 2016年9月8日 下午3:18:48
     * @return: BaseResult
     */
    @RequestMapping("/sureOrder")
    public
    @ResponseBody
    BaseResult sureOrder(CancelOrder sure, String comment, String comResult) {
        OrderCommentVo orderCommentVo = new OrderCommentVo();
        orderCommentVo.setOrderId(sure.getOrderid());
        orderCommentVo.setUserId(sure.getUserid());
        orderCommentVo.setComment(comment);
        orderCommentVo.setComResult(comResult);
        sure.setOrderComment(comResult);
        sure.setOrderCommentRemark(comment);
        buyerOrderService.sureOrder(sure);
        orderCommentService.insertOrdercomment(orderCommentVo);
        if ("0".equals(comResult)) {
            try {
                OperMessage operMessage = new OperMessage();
                operMessage.setCreatetime(new Date());
                operMessage.setDelflag("1");
                operMessage.setStatus("1");
                operMessage.setUserid(Long.valueOf(sure.getUserid()));
                operMessage.setOrderid(Long.valueOf(sure.getOrderid()));
                operMessage.setType(OperMessage.TYPE_ORDER_BAD_COMMENT);
                operMessage.setSource(OperMessage.RESOURCE_BUYER);
                operMessage.setIsread("1");
                operMessageService.insertSelective(operMessage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return BaseResult.success();
    }

    /**
     * @param order
     * @return
     * @Title: addOrder
     * @Description: 买家下单
     * @author: Administrator
     * @date: 2016年9月7日 上午9:48:13
     * @return: BaseResults
     */
    @RequestMapping(value = {"addOrder"}, method = RequestMethod.POST)
    @ResponseBody
    public BaseResult addOrder(OrderInfoVo order) {
        BaseResult result = null;
        try {
            result = buyerOrderService.addOrder(order);
        } catch (Exception e) {
            return BaseResult.fail("0001",e.getMessage());
        }
        List<OrderInfoVo> orders = (List<OrderInfoVo>) result.getData();
        try {
            OperMessage operMessage = new OperMessage();
            operMessage.setCreatetime(new Date());
            operMessage.setDelflag("1");
            operMessage.setStatus("1");
            operMessage.setUserid(Long.valueOf(order.getUserId()));
            operMessage.setOrderid(Long.valueOf(orders.get(0).getId()));
            operMessage.setType(OperMessage.TYPE_ORDER);
            operMessage.setSource(OperMessage.RESOURCE_BUYER);
            operMessage.setIsread("1");
            operMessageService.insertSelective(operMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseResult.success();
    }

    @RequestMapping(value = {"getBuyerInfo"}, method = RequestMethod.GET)
    @ResponseBody
    public BaseResult getBuyerInfo(String userId) {
        return buyerOrderService.getBuyerInfo(userId);
    }
    @RequestMapping("/changeName")
    @ResponseBody
    public BaseResult changeName(String userId,String buyerName){
        this.buyerOrderService.changeName(userId,buyerName);
        return BaseResult.success();
    }

}
